package com.lt.controller;

import com.lt.entity.Notice;
import com.lt.service.NoticeService;
import com.lt.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

/**
 * @Author :liteng
 * @Time : 2023/10/29 20:22
 */

@RestController
@Slf4j
@RequestMapping("/notice")
public class NoticeController {

    private final NoticeService noticeService;

    public NoticeController(NoticeService noticeService) {
        this.noticeService = noticeService;
    }

    /**
     * 查询公告
     *
     * @param start
     * @param pageSize
     * @param key
     * @param dept
     * @param dateTime
     * @return
     */
    @GetMapping
    public Result getNotice(Integer start, Integer pageSize, String key, Integer dept, LocalDateTime dateTime) {
        List<Notice> notice;
        int pageStart = pageSize * (start - 1);
        log.info("开始执行：" + pageStart + pageSize + key + dept + dateTime);
        HashMap<String, Object> map = new HashMap<>();
        if (dept == null) {
            notice = noticeService.getNotices(key, pageStart, pageSize);
            map.put("notices", notice);
            map.put("noticeSum", noticeService.getNoticesNum(key, 0));
        } else {
            notice = noticeService.getNotices(key, dept, pageStart, pageSize);
            map.put("notices", notice);
            map.put("noticeSum", noticeService.getNoticesNum(key, dept));
        }
//        int size = notice.size();

        return Result.success(map);
    }

    /**
     * 删除公告
     *
     * @param id
     * @return
     */
    @DeleteMapping
    public Result deleteNotice(Long id) {

        noticeService.delete(id);
        return Result.success("删除成功");
    }

    /**
     * 查看某一条公告
     *
     * @param id
     * @return
     */
    @GetMapping("/detail")
    public Result getNoticeDetail(Long id) {
        Notice one = noticeService.getOne(id);
        return Result.success(one);
    }
}
